package com.hnust.service.impl;

import com.hnust.dao.*;
import com.hnust.domain.Class;
import com.hnust.domain.CollegeAdmin;
import com.hnust.domain.record.CollegeAdminRecord;
import com.hnust.domain.record.SystemMessage;
import com.hnust.service.AccountService;
import com.hnust.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private CollegeAdminDao collegeAdminDao;

    @Autowired
    private ClassDao classDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private CourseGroupDao courseGroupDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private CollegeDao collegeDao;

    /**
     * 获取系统信息
     * @param collegeId 学院ID
     * @param adminId 管理员ID
     * @return
     */
    @Override
    public SystemMessage getSystemMessage(String collegeId, String adminId) {

        Integer studentCount=0;
        List<Class> classList=classDao.findClassByCollegeId(collegeId);
        for(Class cla : classList){
            studentCount=studentCount+studentDao.findStudentCountByClassId(cla.getClaId());
        }
        //调用dao层获取所有数据
        Integer teacherCount=teacherDao.findTeacherCountByCollegeId(collegeId);
        Integer courseCount=Integer.valueOf(courseDao.getCourseNum(collegeId));
        Integer courseGroupCount=courseGroupDao.findCourseGroupCountByCollegeId(collegeId);
        Integer classCount=classDao.findClassCountByCollegeId(collegeId);
        Integer majorCount=majorDao.findMajorCountByCollegeId(collegeId);
        CollegeAdmin collegeAdmin=collegeAdminDao.findCollegeAdminByAdminId(adminId);
        String collegeName=collegeDao.findNameByCollegeId(collegeAdmin.getColId());
        CollegeAdminRecord collegeAdminRecord=new CollegeAdminRecord();
        collegeAdminRecord.setAccount(collegeAdmin.getAccount());
        collegeAdminRecord.setCollege(collegeName);
        collegeAdminRecord.setCollegeAdminId(collegeAdmin.getColAdmId());
        collegeAdminRecord.setCollegeId(collegeAdmin.getColId());
        collegeAdminRecord.setName(collegeAdmin.getName());
        collegeAdminRecord.setPassword(collegeAdmin.getPassword());

        return new SystemMessage(studentCount,teacherCount,courseCount,
                courseGroupCount,classCount,majorCount,collegeAdminRecord);
    }

    /**
     * 修改管理员密码
     * @param adminId
     * @param newPassword
     * @return
     */
    @Override
    public Boolean changePassword(String adminId, String newPassword) {
        return collegeAdminDao.updatePasswordByAdminID(adminId,newPassword);
    }
}
